class ListNode {
      int val;
    ListNode next;
    ListNode() {}
     ListNode(int val) {
        this.val = val;
    }
     ListNode(int val, ListNode next) {
        this.val = val; this.next = next;
    }
 }

public class removeElements {
    public ListNode removeElements(ListNode head, int val) {
        if(head == null){
            return null;
        }
        ListNode prev = head;
        ListNode cur = head.next;

        while(cur != null){
            if(cur.val == val){
                prev.next = cur.next;
            }else{
                prev = prev.next;
            }
            cur = cur.next;
        }
        if(head.val == val){
            head = head.next;
        }
        return head;
    }
}